﻿using log4net;
using System;
using System.Text;
using System.Windows;

namespace WebSocketAndPrint
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static MainWindow GetWin;
        private ILog log = log4net.LogManager.GetLogger("WebSocket");
        private WebSocketHandle SocketHandle = new WebSocketHandle();
        ViewData VD = ViewData.GetInstance();



        public MainWindow()
        {
            MainWindow.GetWin = this;
            InitializeComponent();

            //数据绑定
            this.DataContext = VD;
            VD.IP = IpUtil.GetLocalIP();
            VD.Port = "6789";
            VD.RichTextBoxObj = this.ChatContent;

            //开启 WebSocket
            SocketHandle.Start("ws://" + VD.IP + ":" + VD.Port);
        }


        //发送消息
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (VD.SelectSocket == null)
            {
                MessageBox.Show("请先选择给谁发送！");
                return;
            }

            String msg = this.InputMessage.Text.Trim();
            if (msg == "")
            {
                MessageBox.Show("不能发送空信息！");
                return;
            }
            VD.SelectSocket.WebSocketConnection.Send(msg);
            this.InputMessage.Text = "";
            //this.ChatContent.AppendText("asdfs\r\naf");            
        }


        //下载HTML客户端
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            String html = "<!DOCTYPE>\n" +
                "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" +
                "<head>\n" +
                "\t<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n" +
                "    <title>websocket client</title>\n" +
                "    <script type=\"text/javascript\">\n" +
                "        var start = function () {\n" +
                "            var inc = document.getElementById('incomming');\n" +
                "            var wsImpl = window.WebSocket || window.MozWebSocket;\n" +
                "            var form = document.getElementById('sendForm');\n" +
                "\t\t\tvar rename = document.getElementById('rename');\n" +
                "\t\t\tvar getImg = document.getElementById('getImg');\n" +
                "            var input = document.getElementById('sendText');\n" +
                "\n" +
                "            inc.innerHTML += \"connecting to server ..<br/>\";\n" +
                "\n" +
                "            // create a new websocket and connect\n" +
                "            window.ws = new wsImpl('ws://" + VD.IP + ":" + VD.Port + "');\n" +
                "\n" +
                "            // when data is comming from the server, this metod is called\n" +
                "            ws.onmessage = function (evt) {\n" +
                "\t\t\t\tvar msg =  JSON.parse(evt.data);\n" +
                "\t\t\t\tswitch(msg.Type){\n" +
                "\t\t\t\t\tcase \"msg\":\n" +
                "\t\t\t\t\tcase \"rename\":\n" +
                "\t\t\t\t\t\tinc.innerHTML += msg.Text + '<br/>';\n" +
                "\t\t\t\t\t\tbreak;\t\t\t\t\t\t\n" +
                "\t\t\t\t\tcase \"getImg\":\n" +
                "\t\t\t\t\t\tinc.innerHTML += \"收到图片\" + '<br/>';\t\t\t\t\t\t\n" +
                "\t\t\t\t\t\tvar img = new Image();//创建img容器\n" +
                "\t\t\t\t\t\timg.src=\"data:image/png;base64,\" + msg.Text;\n" +
                "\t\t\t\t\t\timg.width = 500;\n" +
                "\t\t\t\t\t\tdocument.body.appendChild(img);\n" +
                "\t\t\t\t\t\tbreak;\n" +
                "\t\t\t\t\tdefault:\n" +
                "\t\t\t\t\t\talert(\"未处理的消息内容！\\n\" + evt.data);\n" +
                "\t\t\t\t}\n" +
                "                \n" +
                "            };\n" +
                "\n" +
                "            // when the connection is established, this method is called\n" +
                "            ws.onopen = function () {\n" +
                "                inc.innerHTML += '.. connection open<br/>';\n" +
                "            };\n" +
                "\n" +
                "            // when the connection is closed, this method is called\n" +
                "            ws.onclose = function () {\n" +
                "                inc.innerHTML += '.. connection closed<br/>';\n" +
                "            }\n" +
                "\n" +
                "            form.addEventListener('submit', function (e) {\n" +
                "                e.preventDefault();\n" +
                "                var val = input.value;\n" +
                "\t\t\t\tvar msg = {type:\"msg\",text:val};\n" +
                "                ws.send(JSON.stringify(msg));\n" +
                "                input.value = \"\";\n" +
                "            });\n" +
                "\t\t\t\n" +
                "\t\t\trename.addEventListener(\"click\",function(e){\n" +
                "\t\t\t\te.preventDefault();\n" +
                "                var val = input.value;\n" +
                "\t\t\t\tvar msg = {type:\"rename\",text:val};\n" +
                "                ws.send(JSON.stringify(msg));\n" +
                "                input.value = \"\";\n" +
                "\t\t\t});\n" +
                "\n" +
                "\t\t\tgetImg.addEventListener(\"click\",function(e){\n" +
                "\t\t\t\te.preventDefault();\n" +
                "\t\t\t\tvar msg = {type:\"getImg\",text:\"\"};\n" +
                "                ws.send(JSON.stringify(msg));\n" +
                "\t\t\t});\n" +
                "        }\n" +
                "        window.onload = start;\n" +
                "    </script>\n" +
                "</head>\n" +
                "<body>\n" +
                "    <form id=\"sendForm\">\n" +
                "        <input id=\"sendText\" placeholder=\"Text to send\" />\n" +
                "    </form>\n" +
                "    <pre id=\"incomming\"></pre>\n" +
                "\t<button id=\"rename\">改名</button>\n" +
                "\t<button id=\"getImg\">获取图片</button>\n" +
                "</body>\n" +
                "</html>";
            String FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\socket.html";
            System.IO.File.WriteAllText(FilePath, html, Encoding.UTF8);
            MessageBox.Show(FilePath, "保存路径");
            System.Diagnostics.Process.Start(FilePath);
        }
    }
}
